package endurance.utils;

import endurance.observer.Observer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Author: sunlin
 * @Date: 2024-05-17 11:21
 * @Version: 1.0
 * @Description:
 */
@Component
public class ClassUtils {

    @Autowired
    private ApplicationContext applicationContext;

    // 获取接口所有实现类的首字母小写名称
    public List<String> getImplementationClassNames(Class<?> interfaceClass) {
        List<String> implementationClassNames = new ArrayList<>();

        // 获取所有 Bean 的名称
        Map<String, Object> beans = applicationContext.getBeansWithAnnotation(Component.class);

        for (Object bean : beans.values()) {
            if (interfaceClass.isAssignableFrom(bean.getClass()) && !bean.getClass().isInterface()) {
                String className = bean.getClass().getSimpleName();
                String lowercaseName = className.substring(0, 1).toLowerCase() + className.substring(1);
                implementationClassNames.add(lowercaseName);
            }
        }

        return implementationClassNames;
    }

}
